package ua.eug.robov2.schematic;

import ua.eug.robov2.ImageProvider;
import ua.eug.robov2.menus.SideMenu;
import ua.eug.robov2.utils.Vector2i;

import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Touchable;
import com.badlogic.gdx.scenes.scene2d.utils.ActorGestureListener;

// placing chip to sheet
public class SheetPlacer extends Actor {
	
	// sensitive border for sheet moving
	private final static int BORDERS = 5;
	
	// TODO
	private final static float SCALE = 8;
	
	private SheetActor sheet;
	private SideMenu menu;
	private TextureRegion texture;
	private Vector2i pn = new Vector2i();
	private Vector2i scr;
	private Boolean moving = false;
	private float dx, dy;

	public SheetPlacer(SheetActor sheet) {
		super();
		this.sheet = sheet;
		this.menu = SideMenu.getInstance();
		
        addListener(new ActorGestureListener(){
			public void pan (InputEvent event, float x, float y, float deltaX, float deltaY) {
				// TODO resize
				event.getTarget().moveBy(deltaX, deltaY);
			}
        });
        
        dx = 0;
        dy = 0;
        this.hideChip();
	}

	public void moveBy(float deltaX, float deltaY) {
		dx += deltaX;
		dy += deltaY;
		//System.out.println("dx "+dx+" dy "+dy);
		if (Math.abs(dx) >= SCALE || Math.abs(dy) >= SCALE) {
			pn.add((int)(dx/SCALE), (int)(dy/SCALE));
			dx = dx%SCALE;
			dy = dy%SCALE;
			
			moving = true;
		}
	}
	
	public void hideChip() {
    	sheet.setTouchable(Touchable.enabled);
    	this.setTouchable(Touchable.disabled);
    	this.setVisible(false);    	
    }

    public void showChip (String name) {
    	texture = ImageProvider.FindRegion(name);
		this.setName(name);	
		sheet.initFloat(name);
    	this.setBounds(0,0,texture.getRegionWidth(),texture.getRegionHeight());
    	pn = sheet.getCenter().sub(4,4);
    	scr = sheet.getScrSize();
    	System.out.println("pn= "+pn);
    	moving = true;
    	this.setTouchable(Touchable.enabled);
    	sheet.setTouchable(Touchable.disabled);
    	this.setVisible(true);
    }

	@Override
    public void draw(Batch batch, float alpha){
        batch.draw(texture,this.getX(),this.getY());
    }

    @Override
    public void act(float delta){
    	if (moving) {
    		moving = false;
    		Vector2i or = sheet.getOrigin();
    		setX((pn.x - or.x) * SCALE);
    		setY((pn.y - or.y) * SCALE);
    		
    		// check if free
    		Boolean isFree = sheet.setFloatPos( pn );
    		// disable placing on busy
    		menu.setState("yes", isFree);
    		
    		// moving sheet
    		Vector2i pan = new Vector2i();
    		if (pn.x - BORDERS < or.x) {
    			pan.add(-1, 0);   			
    		} else if (pn.x + BORDERS >= or.x + scr.x) {
    			pan.add(1, 0);
    		}

    		if (pn.y - BORDERS < or.y) {
    			pan.add(0, -1);
    		} else if (pn.y + BORDERS >= or.y + scr.y) {
    			pan.add(0, 1);
    		}
    		sheet.pan(pan);
    	}
    }

}
